#!/bin/bash

set -e

CONFIG_OPTIONS="no-autoalginit no-autoerrinit no-autoload-config no-capieng
no-cms no-comp no-ct no-deprecated no-dso no-dynamic-engine no-engine no-err
no-filenames no-gost no-hw-padlock no-ocsp no-psk no-shared no-srp no-tests
no-ts no-ui no-zlib"

# Disable SSLv2 & SSLv3
CONFIG_OPTIONS="$CONFIG_OPTIONS no-ssl2 no-ssl2-method no-ssl3 no-ssl3-method"

# Disable algorithms
CONFIG_OPTIONS="no-aria no-bf no-blake2 no-camellia no-cast no-cmac no-des
no-dsa no-idea no-md4 no-mdc2 no-ocb no-rc2 no-rc4 no-rmd160 no-scrypt no-seed
no-siphash no-sm2 no-sm3 no-sm4 no-whirlpool"

here=$(dirname $(realpath "$0" 2> /dev/null || grealpath "$0"))
. "$here"/base.sh || (echo "Could not source contrib/base.sh" && exit 1)

setup_pkg "openssl" "https://github.com/openssl/openssl.git" "830bf8e1e4749ad65c51b6a1d0d769ae689404ba" $here

if ! [ -d dist ] ; then
    (
        case $BUILD_TYPE in
            linux)
                ./config --prefix="$pkgbuilddir/dist" $CONFIG_OPTIONS
                ;;
            darwin)
                ./Configure darwin64-$(uname -m)-cc --prefix="$pkgbuilddir/dist" $CONFIG_OPTIONS
                ;;
            wine)
                ./Configure --cross-compile-prefix=${GCC_TRIPLET_HOST}- mingw --prefix="$pkgbuilddir/dist" $CONFIG_OPTIONS
                ;;
        esac
    ) || fail "Could not configure $pkgname. Please make sure you have a C compiler installed and try again."
    make depend || fail "Could not build $pkgname"
fi
make -j$WORKER_COUNT || fail "Could not build $pkgname"
make install_sw || fail "Could not install $pkgname"
popd_pkg
